/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/14
 * Time: 03:52
 */
$(function(){
    var ue = UE.getEditor('editor', {initialFrameHeight: 250});
    sendAjax('/v1/regions.json', 'get', {}, function(json) {
        var data = json.body.rows;
        if (data.length > 0) {
            $.each(data, function(n, v) {
                var option = '<option value="'+ v.id+'">'+ v.name+'</option>';
                $('.apart-region').append(option);
            });
        }
        $.get('/v1/standards.json', {}, function(e) {
            var json = JSON.parse(e);
            var data = json.body.rows;
            if (data.length > 0) {
                $.each(data, function(n, v) {
                    var option = '<option value="'+ v.id+'">'+ v.name+'</option>';
                    $('.apart-standard').append(option);
                });
            }
        });
    });

    $('.btn-save').on('click', function() {
        $('[name="other"]').val(ue.getContent());
        var data = {
            name : $('.apart-name').val(),
            region : $('.apart-region').val(),
            type : $('.apart-type').val(),
            address : $('.apart-address').val(),
            area : $('.apart-area').val(),
            standard : $('.apart-standard').val(),
            orientations : $('.apart-orientations').val(),
            rent : $('.apart-rent').val(),
            other : $('.apart-other').val(),
            status : $('.apart-status').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/save/apart', 'post', {data:data}, function(json) {
                var div = '<div><input type="hidden" class="jump-url" value="/manager/apart.html">' + json.message + '</div>';
                $('#action-tip .modal-body .tip-content').html(div);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        if ($(this).find('.jump-url').val()) {
            location.href = $(this).find('.jump-url').val();
        }
        $(this).find('.tip-content').text('');
    });
});

function checkForm(data) {
    if (!data.name) {
        $('#action-tip .modal-body .tip-content').text('请填写房源名称');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.region) {
        $('#action-tip .modal-body .tip-content').text('请选择所在地');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.type) {
        $('#action-tip .modal-body .tip-content').text('请选择类型');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.address) {
        $('#action-tip .modal-body .tip-content').text('请填写地址');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.area) {
        $('#action-tip .modal-body .tip-content').text('请选择面积');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.standard) {
        $('#action-tip .modal-body .tip-content').text('请选择户型');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.orientations) {
        $('#action-tip .modal-body .tip-content').text('请选择朝向');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.rent) {
        $('#action-tip .modal-body .tip-content').text('请填写月租金');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}